<h1>Review Offers</h1>

<?php $display = 'block'; ?>
<?php $i = 1; ?>
<?php foreach($offers as $offer): ?>
  <div class="offer-review clearfix" id="offer-<?php echo $i ?>" style="display: <?php echo $display ?>;">
    <div class="count">
      <?php echo $i.'/'.count($offers) ?>
    </div>
    <br />
    <form id="form_<?php echo $offer->getId() ?>">
      <div class="title">
        <?php echo $offer->getName() ?>
        <br /><a href="<?php echo $offer->getUrl() ?>" target="_blank"><?php echo $offer->getUrl() ?></a>
      </div>
      <div style="margin: 10px; font-size: 16px; font-weight: bold;">
        <input style="text-align: right; width: 30px; font-size: 16px; font-weight: bold;" type="text" name="discount" value="<?php echo $offer->getDiscountPercent() ?>" /> %
      </div>
      <div class="categories clearfix">
        <input type="hidden" name="id" value="<?php echo $offer->getId() ?>" />
        <?php $offer_categories = array() ?>
        <?php foreach($offer->getOfferCategorys() as $offer_category): ?>
          <input type="hidden" name="offer_categories_<?php echo $offer->getId() ?>[]" id="offer_categories_<?php echo $offer->getId() ?>_<?php echo $offer_category->getCategoryId() ?>" value="<?php echo $offer_category->getCategoryId() ?>" />
          <?php $offer_categories[] = $offer_category->getCategoryId() ?>
        <?php endforeach; ?>
        <?php foreach($categories as $category): ?>
          <?php $class = "" ?>
          <?php if(!in_array($category->getId(), $offer_categories)): ?>
            <?php $class = "off" ?>
          <?php endif; ?>
          <a href="#" class="<?php echo $class ?>" onclick="toogleCategory(this, <?php echo $offer->getId() ?>, <?php echo $category->getId() ?>); return false;"><?php echo $category->getName() ?></a>
        <?php endforeach; ?>
      </div>
    </form>
    <div style="float: right">
      <a class="button-off" href="#" onclick="showPrevOffer(<?php echo $i ?>); return false;"><<</a>
      <a class="button-off" href="#" onclick="showNextOffer(<?php echo $i ?>); showNextOffer(<?php echo $i ?>); return false;">>></a>
    </div>
    <a class="button-off" href="#" onclick="showNextOffer(<?php echo $i ?>); return false;">skip</a>
    <a class="button" href="#" onclick="saveOffer(<?php echo $offer->getId() ?>); showNextOffer(<?php echo $i ?>); return false;">save</a>
  </div>
  <?php $display = 'none'; ?>
  <?php $i++; ?>
<?php endforeach; ?>

<script type="text/javascript">
  var showNextOffer = function(index)
  {
    if(index < <?php echo count($offers) ?>)
    {
      $('#offer-'+index).hide();
      $('#offer-'+(index+1)).show();
    }
  }
  var showPrevOffer = function(index)
  {
    if(index > 1)
    {
      $('#offer-'+index).hide();
      $('#offer-'+(index-1)).show();
    }
  }
  var toogleCategory = function(obj, oid, cid)
  {
    if($(obj).hasClass('off'))
    {
      $(obj).removeClass('off');
      $('#form_'+oid).append('<input type="hidden" name="offer_categories_'+oid+'[]" id="offer_categories_'+oid+'_'+cid+'" value="'+cid+'" />');
    }
    else
    {
      $(obj).addClass('off');
      $('#offer_categories_'+oid+'_'+cid).remove();
    }
  }
  var saveOffer = function(oid)
  {
    $.ajax({
      url: "<?php echo url_for('offer/ajaxreview') ?>",
      type: 'post',
      data: $('#form_'+oid).serialize(),
      success: function(){
        
      }
    });
  }
</script>
